<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地名" prop="village">
        <el-input
          v-model="queryParams.village"
          placeholder="请输入地名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['dgw:vaccine:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['dgw:vaccine:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['dgw:vaccine:remove']"
              >删除</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dgw:vaccine:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5" :push="20">
       <span v-model="Sum" style="color: #ffffff; float: right" >应免:&nbsp;{{Sum}}&nbsp;头</span>

      </el-col>
      <el-col :span="1.5" :push="16">
       <span  style="color: #ffffff; float: right" >已免:&nbsp;{{Sum}}&nbsp;头</span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="24">
      <el-col :span="4">
          <el-table v-loading="loading"height="700" @row-click="selectRow" :row-style="rowStyle" :data="deptList" @selection-change="handleSelectionChange">
            <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
            <el-table-column label="乡镇" align="center" prop="deptName"/>


            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  @click="getSum(scope.row)"
                >查看</el-button>
                <!--                    <el-button
                                      size="mini"
                                      type="text"
                                      icon="el-icon-delete"
                                      @click="handleDelete(scope.row)"
                                      v-hasPermi="['dgw:vaccine:remove']"
                                    >删除</el-button>-->
              </template>
            </el-table-column>
          </el-table>
      </el-col>
<!--   :data="vaccineSumList"   -->
      <el-col :span="4">
        <el-table v-loading="loading" height="700"  :row-style="rowStyleU" :data="deptNameList"  @selection-change="handleSelectionChange">

          <el-table-column label="地名" align="center" prop="village"/>
          <el-table-column label="用户数量" align="center" prop="user_sum"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="getUser(scope.row)"
              >查看用户<i class="el-icon-arrow-right el-icon--right"></i></el-button>
              <!--                    <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['dgw:vaccine:remove']"
                                  >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="16">
        <el-table v-loading="loading" height="700"  :data="deptNameUserList" @selection-change="handleSelectionChange">




          <el-table-column type="selection" width="55" align="center"/>
          <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
          <el-table-column label="养殖户" align="center" prop="userName"/>
          <el-table-column label="电话" align="center" prop="phone"/>
          <el-table-column label="口蹄疫苗" align="center">
            <el-table-column label="免疫数量(口蹄1)" align="center" prop="footMouthSum"/>
            <el-table-column label="免疫数量(口蹄2)" align="center" prop="footMouthSum"/>
            <el-table-column label="免疫时间" align="center" prop="footMouthTime" width="120">
              <template slot-scope="scope">
                <el-tag type="success">{{ parseTime(scope.row.footMouthTime, '{y}-{m}-{d}') }}</el-tag>
              </template>
            </el-table-column>
          </el-table-column>



          <el-table-column label="布鲁氏菌病疫苗" align="center">
            <el-table-column label="检疫数量" align="center" prop="brucellosisSum"/>
            <el-table-column label="检疫时间" align="center" prop="brucellosisTime" width="120">
              <template slot-scope="scope">
                <el-tag type="success">{{ parseTime(scope.row.brucellosisTime, '{y}-{m}-{d}') }}</el-tag>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="炭疽疫苗" align="center">
            <el-table-column label="检疫数量" align="center" prop="anthraxSum"/>
            <el-table-column label="检疫时间" align="center" prop="anthraxTime" width="120">
              <template slot-scope="scope">
                <el-tag type="success">{{ parseTime(scope.row.anthraxTime, '{y}-{m}-{d}') }}</el-tag>
              </template>
            </el-table-column>
          </el-table-column>



          <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['dgw:vaccine:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['dgw:vaccine:remove']"
                    >删除</el-button>
                  </template>
                </el-table-column>-->
        </el-table>
      </el-col>
    </el-row>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getQueryDeptUser"
    />

    <!-- 添加或修改疫苗对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主键" prop="id">
          <el-input v-model="form.id" placeholder="请输入主键"/>
        </el-form-item>
        <el-form-item label="养殖用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入养殖用户id"/>
        </el-form-item>
        <el-form-item label="口蹄疫苗注射时间" prop="aftosaTime">
          <el-date-picker clearable size="small"
                          v-model="form.aftosaTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择口蹄疫苗注射时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="炭菌疫苗注射时间" prop="anthraxTime">
          <el-date-picker clearable size="small"
                          v-model="form.anthraxTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择炭菌疫苗注射时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="布菌疫苗注射时间" prop="clothbacteriaTime">
          <el-date-picker clearable size="small"
                          v-model="form.clothbacteriaTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择布菌疫苗注射时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="口蹄疫苗" prop="aftosa">
          <el-select v-model="form.aftosa" placeholder="请选择已注射或未注射" clearable size="small">
            <el-option label="未注射" :value="1"></el-option>
            <el-option label="已注射" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="炭菌疫苗" prop="anthrax">
          <el-select v-model="form.anthrax" placeholder="请选择已注射或未注射" clearable size="small">
            <el-option label="未注射" :value="1"></el-option>
            <el-option label="已注射" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="布菌疫苗" prop="clothbacteria">
          <el-select v-model="form.clothbacteria" placeholder="请选择已注射或未注射" clearable size="small">
            <el-option label="未注射" :value="1"></el-option>
            <el-option label="已注射" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="养殖户" prop="breedName">
          <el-input v-model="form.breedName" placeholder="请输入养殖户"/>
        </el-form-item>
        <el-form-item label="耳标编号" prop="earNumber">
          <el-input v-model="form.earNumber" placeholder="请输入耳标编号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVaccine,
  getVaccine,
  delVaccine,
  addVaccine,
  updateVaccine,
  exportVaccine,
  querySum,
} from '@/api/dgw/vaccine'
import {queryDeptCun,queryDeptName} from '@/api/system/dept'
import {listQueryVillage,listQueryVillageUser,queryVSum} from '@/api/dgw/registration'
export default {
  name: 'Vaccine',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      Sum:null,
      // 疫苗表格数据
      vaccineList: [],
      deptList:[],
      deptNameList:[],
      deptNameUserList:[],
      name:null,
      names:null,
      //免疫总量
      vaccineSumList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      defaultProps:{
        children: 'children',
        label: 'deptName'
    },
      // 查询参数
       queryParams: {
        pageNum: 1,
        pageSize: 20,
        village:null,
      },
      queryParam:{
        villagesTowns:null,
      },
      queryParamUser:{
        village:null,
        villagesTowns:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.querySumList()
    this.getQueryDeptCun()
  },
  methods: {
    /** 查询疫苗列表 */
    getList() {
     // this.loading = true
     /* listVaccine(this.queryParams).then(response => {
        this.vaccineList = response.rows
        this.total = response.total
        this.loading = false
      })*/
      queryVSum(this.queryParamUser).then(response=>{
        this.Sum = response.data.sum
      })
    },

    getQueryDeptCun(){
      this.loading = true
      queryDeptCun().then(response =>{
        console.log("乡镇数据",response);
        this.deptList = response.data
        this.loading = false
      })
    },
    getQueryDeptName(){
      this.loading = true
      listQueryVillage(this.queryParam).then(response =>{
        console.log("村",response);
        this.deptNameList = response.rows
        this.loading = false
      })
    },
    getQueryDeptUser(){
      this.loading = true
      listQueryVillageUser(this.queryParams).then(response =>{
        console.log("用户",response);
        this.deptNameUserList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        updateBy: null,
        updateTime: null,
        createBy: null,
        createTime: null,
        aftosaTime: null,
        anthraxTime: null,
        clothbacteriaTime: null,
        aftosa: null,
        anthrax: null,
        clothbacteria: null,
        breedName: null,
        earNumber: null,
        nickName: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParamUser.pageNum = 1
      this.getQueryDeptUser()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParamUser.village=null,
        this.queryParamUser.villagesTowns=null,
        this.queryParam.villagesTowns=null,
        this.queryParams.village=null,

        this.names=null
      this.name=null
        this.getQueryDeptName()
      this.handleQuery()
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加疫苗'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getVaccine(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改疫苗'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVaccine(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addVaccine(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除疫苗编号为"' + ids + '"的数据项？').then(function() {
        return delVaccine(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有疫苗数据项？').then(() => {
        this.exportLoading = true
        return exportVaccine(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    querySumList() {
      this.loading = true
      querySum(this.queryParams).then(response => {
        this.vaccineSumList = response.rows,
          this.total = response.total
        this.loading = false
      })

    },
    /*查询村*/
    getSum(row){
      console.log("注射信息",row)
      this.queryParam.villagesTowns= row.deptName;
      this.getQueryDeptName()
      this.queryParamUser.villagesTowns =row.deptName;
      this.queryParamUser.village=null;
      this.getList()

    },
    getUser(row){
      this.queryParams.village=row.village
      this.names = row.village
      this.getQueryDeptUser()
      this.queryParamUser.village=row.village;
      this.queryParamUser.villagesTowns = null
      this.getList()
    },
    // 行鼠标点击事件
    selectRow(row, column, event) {
      console.log(row);
      console.log(column);
      console.log(event);
      this.name = row.deptName;
    },
    // 更改选中行背景色
    rowStyle({ row }) {
      if (this.name === row.deptName) {
        return { 'background-color': '#fae100', cursor: 'pointer' };
      }
      return { cursor: 'pointer' };
    },
    rowStyleU({ row }) {
      if (this.names === row.village) {
        return { 'background-color': '#fae100', cursor: 'pointer' };
      }
      return { cursor: 'pointer' };
    },
  }
}
</script>
